package com.mma.assigment_registrationform;

import java.util.HashMap;
import java.util.Map;

import android.media.audiofx.AcousticEchoCanceler;
import android.widget.EditText;

public class DataManager {
	private int userID = 0;
	private static DataManager instance = null;

	private Map<String, UserAccount> users = null;

	private DataManager() {
		this.users = new HashMap<String, UserAccount>();
		UserAccount admin = new UserAccount();
		admin.setPassword("admin");
		admin.setUserName("admin");
		this.users.put("admin", admin);

	}

	public static DataManager getInstance() {
		if (instance == null) {
			instance = new DataManager();
		}

		return instance;
	}

	public boolean isUserExists(String username) {
		return users.keySet().contains(username);
	}

	public void addUser(String usernameValue, UserAccount account) {
		++userID;
		account.setUID(userID);
		users.put(usernameValue, account);

	}

	public boolean isValidUser(String username, String password) {

		if (users.keySet().contains(username)) {
			if (users.get(username).getPassword().equals(password)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}

	}

	public UserAccount getUser(String username) {
		if (isUserExists(username)) {
			return users.get(username);
		} else {
			return null;
		}
	}

	public void modifyUser(String username, String passwordValue,
			String cityValue, String emailValue) {
		users.get(username).setAddress(cityValue);
		users.get(username).setPassword(passwordValue);
		users.get(username).seteMail(cityValue);
	}

	public boolean hasUserWithID(int id) {
		for (String user : users.keySet()) {
			if (users.get(user).getUID() == id) {
				return true;
			}
		}
		return false;
	}

	public String getUser(int id) {
		String userName = "";
		for (String user : users.keySet()) {
			if (users.get(user).getUID() == id) {
				userName = user;
				break;
			}
		}

		return userName;
	}

}
